package com.moss.cloud.base.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moss.cloud.base.api.model.from.UserLockFrom;
import com.moss.cloud.base.api.model.from.UserUnlockFrom;
import com.moss.cloud.base.biz.mapper.AccountLockRecordMapper;
import com.moss.cloud.base.biz.model.AccountLockRecord;
import com.moss.cloud.base.biz.service.IAccountLockRecordService;
import com.moss.cloud.common.core.enums.state.LockAcct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * 账号锁定记录业务实现类
 *
 * @author 瑾年
 * @date 2023年4月6日
 */
@Slf4j
@Service
public class AccountLockRecordServiceImpl extends ServiceImpl<AccountLockRecordMapper, AccountLockRecord> implements IAccountLockRecordService {
    /**
     * 新增用户账号锁定记录
     *
     * @param lockTime
     * @param userLockFrom
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdateLockAccount(LocalDateTime lockTime, UserLockFrom userLockFrom) {
        AccountLockRecord accountLockRecord = this.findByUserId(userLockFrom.getUserId());
        return Objects.isNull(accountLockRecord) ? this.saveRecord(lockTime, userLockFrom) :
                updateLockStatus(accountLockRecord, lockTime);
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean saveRecord(LocalDateTime lockTime, UserLockFrom userLockFrom) {
        AccountLockRecord accountLockRecord = new AccountLockRecord();
        BeanUtils.copyProperties(userLockFrom, accountLockRecord);
        accountLockRecord.setLockTime(lockTime);
        accountLockRecord.setLockState(LockAcct.LOCK.code());
        return this.save(accountLockRecord);
    }

    /**
     * 修改锁定状态
     *
     * @param accountLockRecord
     * @param lockTime
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean updateLockStatus(AccountLockRecord accountLockRecord, LocalDateTime lockTime) {
        accountLockRecord.setLockState(LockAcct.LOCK.code());
        accountLockRecord.setLockTime(lockTime);
        return this.updateById(accountLockRecord);
    }

    /**
     * 根据用户ID查询账号锁定详情
     *
     * @param userId
     * @return
     */
    @Override

    public AccountLockRecord findByUserId(Long userId) {
        return this.getOne(new QueryWrapper<AccountLockRecord>().lambda()
                .eq(AccountLockRecord::getUserId, userId));
    }

    /**
     * 解锁账号
     *
     * @param time
     * @param userUnlockFrom
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean unlockAccount(LocalDateTime time, UserUnlockFrom userUnlockFrom) {
        AccountLockRecord accountLockRecord = this.findByUserId(userUnlockFrom.getUserId());
        accountLockRecord.setUnlockTime(time);
        accountLockRecord.setUnlockReason(userUnlockFrom.getUnlockReason());
        accountLockRecord.setLockState(LockAcct.UNLOCK.code());
        return this.updateById(accountLockRecord);
    }

}
